package com.itheima.consumer.listener;

import jdk.jpackage.internal.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
@Slf4j
public class SpringRabbitListener {
//     利用RabbitListener来声明要监听的队列信息
//     将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
//     可以看到方法体中接收的就是消息体的内容
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg) {
        System.out.println("消费者接收到的消息是：" + msg);
    }
    /**
     * 工作队列模型消费者1
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }
    /**
     * 工作队列模型消费者2
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }
/**
 * 广播模型消费者1
 * @param msg
 */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }
    /**
     * 广播模型消费者2
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }
    /**
     * 路由模型消费者1
     * @param msg
     */
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到direct.queue1的消息：【" + msg + "】");
    }
    /**
     * 路由模型消费者2
     * @param msg
     */
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到direct.queue2的消息：【" + msg + "】");
    }
    /**
     * 通配符模型消费者1
     * @param msg
     */
   @RabbitListener(bindings = @QueueBinding(
           value = @Queue(name = "topic.queue1"),
           exchange = @Exchange(name = "hmall.topic",type = ExchangeTypes.TOPIC),
           key = {"china.#"}
   ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者1接收到topic.queue1的消息：【" + msg + "】");
    }
    /**
     * 通配符模型消费者2
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "hmall.topic",type = ExchangeTypes.TOPIC),
            key = {"*.news"}
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者2接收到topic.queue2的消息：【" + msg + "】");
    }
    @RabbitListener(queues ="object.queue")
    public  void  listenSimpleQueueMessage(Map<String, Object> msg){
        System.out.println("消费者接收到object.queue消息：【" + msg + "】");
    }
    @RabbitListener(queuesToDeclare = @Queue(name= "lazy.queue",
    durable = "true",
    arguments = @Argument(name ="x-queue-mode",value="lazy"))
    )
    public void listenLazyQueue(String msg){
        log.info("接收到 lazy.queue的消息：{}",msg);

    }
    //监听优先级队列
    @RabbitListener(queuesToDeclare = @Queue(
            name="priority.queue",
            durable = "true",
            arguments = @Argument(name = "x-max-priority", value = "10",type="java.lang.Integer")))
    public void listenPriorityQueue(String msg){
        System.out.println("消费者接收到priority.queue消息：【" + msg + "】");
    }
}
